package com.aihna.mall.service.impl;

import com.aihna.common.core.utils.MapstructUtils;
import com.aihna.common.mybatis.core.page.TableDataInfo;
import com.aihna.common.mybatis.core.page.PageQuery;
import com.aihna.mall.domain.StoreProduct;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.aihna.mall.domain.bo.StoreBannerBo;
import com.aihna.mall.domain.vo.StoreBannerVo;
import com.aihna.mall.domain.StoreBanner;
import com.aihna.mall.mapper.StoreBannerMapper;
import com.aihna.mall.service.IStoreBannerService;
import com.aihna.common.core.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 店铺轮播图Service业务层处理
 *
 * @author Joseph.Chen
 * @date 2024-05-17
 */
@RequiredArgsConstructor
@Service
public class StoreBannerServiceImpl implements IStoreBannerService {

    private final StoreBannerMapper baseMapper;

    /**
     * 查询店铺轮播图
     */
    @Override
    public StoreBannerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询店铺轮播图列表
     */
    @Override
    public TableDataInfo<StoreBannerVo> queryPageList(StoreBannerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StoreBanner> lqw = buildQueryWrapper(bo);
        Page<StoreBannerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询店铺轮播图列表
     */
    @Override
    public List<StoreBannerVo> queryList(StoreBannerBo bo) {
        LambdaQueryWrapper<StoreBanner> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StoreBanner> buildQueryWrapper(StoreBannerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StoreBanner> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), StoreBanner::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), StoreBanner::getStatus, bo.getStatus());
        lqw.orderByDesc(StoreBanner::getSort);
        return lqw;
    }

    /**
     * 新增店铺轮播图
     */
    @Override
    public Boolean insertByBo(StoreBannerBo bo) {
        StoreBanner add = MapstructUtils.convert(bo, StoreBanner.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改店铺轮播图
     */
    @Override
    public Boolean updateByBo(StoreBannerBo bo) {
        StoreBanner update = MapstructUtils.convert(bo, StoreBanner.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StoreBanner entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除店铺轮播图
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
